#include "utility.h"



std::string Utility::getCurrentTimeAsString()
{
    std::time_t currentTime = std::time(nullptr);
    std::tm* timeInfo = std::localtime(&currentTime);
    std::stringstream ss;
    ss << timeInfo->tm_year + 1900 << '-' << timeInfo->tm_mon + 1 << '-' << timeInfo->tm_mday << ' '
       << timeInfo->tm_hour << ':' << timeInfo->tm_min << ':' << timeInfo->tm_sec;

    // // 格式化时间为字符串
    //strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", time_info);

    cout<<ss.str()<<endl;
    return ss.str();
}

long long Utility::getCurrentTimeAsLongLong()
{
    std::time_t now = std::time(nullptr);
    cout<<static_cast<long long>(now)<<endl;
    return static_cast<long long>(now);
}

string Utility::longLongToTimeString(long long seconds)
{
    std::time_t t = static_cast<std::time_t>(seconds);
    struct tm *timeinfo = std::localtime(&t);
    char buffer[80];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);

    cout<<std::string(buffer)<<endl;
    return std::string(buffer);
}

std::pair<string, string> Utility::getTimeStrings(long long seconds)
{
    std::time_t t = static_cast<std::time_t>(seconds);
    struct tm* timeinfo = std::localtime(&t);
    std::ostringstream dateStream;
    dateStream << std::setfill('0') << std::setw(4) << (timeinfo->tm_year + 1900) << '-'
               << std::setw(2) << (timeinfo->tm_mon + 1) << '-'
               << std::setw(2) << timeinfo->tm_mday;
    std::ostringstream timeStream;
    timeStream << std::setfill('0') << std::setw(2) << timeinfo->tm_hour << ':'
               << std::setw(2) << timeinfo->tm_min << ':'
               << std::setw(2) << timeinfo->tm_sec;
    return {dateStream.str(), timeStream.str()};
}

void Utility::splitFilePath(const string &filePath)
{
    //C++ 17
    // std::filesystem::path pathObj(filePath);
    // std::string absPath = std::filesystem::absolute(pathObj).string();
    // std::string folderAbsPath = std::filesystem::absolute(pathObj.parent_path()).string();
    // std::string fileNameNoSuffix = pathObj.stem().string();
    // std::string fileType = pathObj.extension().string();
    // std::cout << "文件的绝对路径: " << absPath << std::endl;
    // std::cout << "文件当前所在文件夹绝对路径: " << folderAbsPath << std::endl;
    // std::cout << "文件名不带后缀: " << fileNameNoSuffix << std::endl;
    // std::cout << "文件类型: " << fileType << std::endl;

    char resolvedPath[PATH_MAX];
#ifdef _WIN32
    if (_fullpath(resolvedPath, filePath.c_str(), PATH_MAX)== nullptr) {
#else
    if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
#endif
        std::cerr << "获取绝对路径失败" << std::endl;
        return;
    }
    std::string absPath = resolvedPath;
    size_t lastSlash = absPath.find_last_of('/');
    size_t lastBackSlash = absPath.find_last_of('\\');
    size_t lastSep = (lastSlash!= std::string::npos && lastBackSlash!= std::string::npos)? std::max(lastSlash, lastBackSlash) : (lastSlash == std::string::npos? lastBackSlash : lastSlash);
    std::string folderAbsPath = absPath.substr(0, lastSep);
    size_t lastDot = absPath.find_last_of('.');
    std::string fileNameNoSuffix;
    if (lastDot == std::string::npos || lastDot < lastSep) {
        // 如果没有点或者点在最后一个斜杠之前（表示没有后缀或者是目录名）
        fileNameNoSuffix = absPath.substr(lastSep + 1);
    } else {
        fileNameNoSuffix = absPath.substr(lastSep + 1, lastDot - lastSep - 1);
    }
    std::string fileType;
    if (lastDot!= std::string::npos) {
        fileType = absPath.substr(lastDot+1);
    } else {
        fileType = "";
    }
    std::cout << "文件的绝对路径: " << absPath << std::endl;
    std::cout << "文件当前所在文件夹绝对路径: " << folderAbsPath << std::endl;
    std::cout << "文件名不带后缀: " << fileNameNoSuffix << std::endl;
    std::cout << "文件类型: " << fileType << std::endl;



}

std::pair<string, string> Utility::getCurrentTimeStrings()
{
    std::time_t now = std::time(nullptr);
    struct tm* timeinfo = std::localtime(&now);
    std::ostringstream dateStream;
    dateStream << std::setfill('0') << std::setw(4) << (timeinfo->tm_year + 1900) << '-'
               << std::setw(2) << (timeinfo->tm_mon + 1) << '-'
               << std::setw(2) << timeinfo->tm_mday;
    std::ostringstream timeStream;
    timeStream << std::setfill('0') << std::setw(2) << timeinfo->tm_hour << ':'
               << std::setw(2) << timeinfo->tm_min << ':'
               << std::setw(2) << timeinfo->tm_sec;
    return {dateStream.str(), timeStream.str()};
}

QString Utility::getCurrentTimeAsStringQt()
{
    QDateTime current = QDateTime::currentDateTime();

    qDebug()<<current.toString("yyyy - MM - dd hh:mm:ss");
    return current.toString("yyyy - MM - dd hh:mm:ss");
}

long long Utility::getCurrentTimeInQtAsLongLong()
{
    QDateTime current = QDateTime::currentDateTimeUtc();
    qDebug()<<current.toMSecsSinceEpoch();
    return current.toMSecsSinceEpoch();
}

QString Utility::longLongToTimeStringQt(qint64 milliseconds)
{
    QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(milliseconds);

    qDebug()<<dateTime.toString("yyyy - MM - dd HH:mm:ss");
    return dateTime.toString("yyyy - MM - dd HH:mm:ss");
}

std::pair<QString, QString> Utility::getCurrentTimeStringsQt()
{
    QDateTime now = QDateTime::currentDateTime();
    QString date = now.toString("yyyy - MM - dd");
    QString time = now.toString("hh:mm:ss");
    return {date, time};
}

std::pair<QString, QString> Utility::getTimeStringsQt(long long seconds)
{
    QDateTime dt = QDateTime::fromSecsSinceEpoch(seconds);
    QString date = dt.toString("yyyy - MM - dd");
    QString time = dt.toString("hh:mm:ss");
    return {date, time};
}

void Utility::splitFilePathQt(const QString &filePath)
{
    QFileInfo fileInfo(filePath);
    QString absPath = fileInfo.absoluteFilePath();
    QString folderAbsPath = fileInfo.absoluteDir().absolutePath();
    QString fileNameNoSuffix = fileInfo.completeBaseName();
    QString fileType = fileInfo.suffix();
    qDebug() << "文件的绝对路径: " << absPath;
    qDebug() << "文件当前所在文件夹绝对路径: " << folderAbsPath;
    qDebug() << "文件名不带后缀: " << fileNameNoSuffix;
    qDebug() << "文件类型: " << fileType;
}

Utility::Utility() {
    // getCurrentTimeAsString();
    // getCurrentTimeAsLongLong();

    // longLongToTimeString(getCurrentTimeAsLongLong());
    // splitFilePath("test.txt");

    // std::pair<string, string> result = getTimeStrings(0);
    // cout<<result.first<<endl;
    // cout<<result.second<<endl;

    // getCurrentTimeAsStringQt();
    // getCurrentTimeInQtAsLongLong();

    // longLongToTimeStringQt(getCurrentTimeInQtAsLongLong());

    // QString txtFilePath="test.txt";
    // splitFilePathQt(txtFilePath);
}
